import java.awt.*;
import java.awt.geom.Ellipse2D;

import javax.swing.*;

class GameDisplay extends JComponent{
	
	private static final String INIT = " Let's Start !! ";
	private static final String PAUSE = "|| Pause ||";
	private static final String LOOSE = "GAME OVER";
	private static final int POINT_PIXELS = 7;
	private static final int INTERLINE = 2;
	
	
	private SnakeEngine engine;
	
	
	public GameDisplay(Dimension dim){
		Dimension gridSize = (Dimension) dim.clone();
		gridSize.width=  gridSize.width / (POINT_PIXELS+INTERLINE);
		gridSize.height= gridSize.height / (POINT_PIXELS+INTERLINE);
		engine = new SnakeEngine(gridSize);
	}
	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;
		this.setDoubleBuffered(true);
			
					
		//disegno apple
		
		//Rectangle appleRect = createRectangle(engine.getApple());
		Ellipse2D.Double appleRect = createEllipse(engine.getApple());
		g2d.setColor(Color.RED);
		g2d.draw(appleRect);
		g2d.fill(appleRect);
		
		
		//disegno snake
		g.setColor(Color.GREEN);

		java.util.Iterator<Point> body = engine.getSnake();
		while(body.hasNext()){
			Rectangle snakeTrunk = createRectangle(body.next());
			g2d.draw(snakeTrunk);
			g2d.fill(snakeTrunk);
		}
		
		
		//scritte 
		g2d.setFont(new Font(Font.DIALOG,Font.BOLD, 35));
		FontMetrics font = g2d.getFontMetrics();
		g2d.setColor(Color.ORANGE);
		
		if(engine.loosed()){
			g2d.drawString(LOOSE, (this.getSize().width - font.stringWidth(LOOSE)) /2 , (this.getSize().height - font.getHeight()) /2);
		}else if(!engine.isStarted()){
			g2d.drawString(INIT, (this.getSize().width - font.stringWidth(INIT)) /2 , (this.getSize().height - font.getHeight()) /2);
		}else if(engine.isPaused()){
			g2d.drawString(PAUSE, (this.getSize().width - font.stringWidth(PAUSE)) /2 , (this.getSize().height - font.getHeight()) /2);
		}
		
	}
	
	public static Rectangle createRectangle(Point p){
		int x = p.x * (POINT_PIXELS+INTERLINE);
		int y = p.y * (POINT_PIXELS+INTERLINE);
		return new Rectangle(x,y,POINT_PIXELS,POINT_PIXELS);		
	}
	
	public static Ellipse2D.Double createEllipse(Point p){
		int x = p.x * (POINT_PIXELS+INTERLINE);
		int y = p.y * (POINT_PIXELS+INTERLINE);
		return new Ellipse2D.Double(x,y,POINT_PIXELS,POINT_PIXELS);		
	}
	public SnakeEngine getSnakeEngine(){
		return engine;
	}

}